<?php

/**
 * 后台管理员管理
 * @author  xiaobai
 * @date    2017-01-08
 */

namespace app\panel\controller;

use app\common\controller\Panelbase;
use app\common\model\Admin as admin_model;
use app\common\model\Group as group_model;
use think\helper\Hash;

class Admin extends Panelbase
{

    public function initialize()
    {
        parent::initialize();
        $this->model = new admin_model();
        $this->group = new group_model();
    }

    public function index()
    {
        $count = $this->model->count('id');
        $list = $this->model->paginate(10);

        $this->assign('list', $list);
        return $this->fetch();
    }

    public function add()
    {
        $group = $this->group->column('name','id');

        $this->assign('group', $group);
        return $this->fetch();
    }

    public function add_post()
    {
        if (request()->isPost()) {
            $data = input('post.');

            // 数据验证
            $result = $this->validate($data, 'admin');
            if (true !== $result) {
                $this->error($result);
            }
            if (!empty($data['group_id'])) {
                $data['group_id'] = implode(',', $data['group_id']);
            }
            if ($this->model->allowField(true)->save($data)) {
                $this->success('保存成功', Url('index'));
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $this->error('请求错误');
        }
    }

    public function edit()
    {
        $id = input("param.id");
        $row = $this->model->where('id',$id)->find()->append(['group_id'])->toArray();
        if (empty($row)) {
            $this->error('数据不存在');
        }

        $group = $this->group->column('name','id');

        $this->assign($row);
        $this->assign('group', $group);

        return $this->fetch();
    }

    public function edit_post()
    {
        if (request()->isPost()) {
            $data = input('post.');
            // 数据验证
            $result = $this->validate($data, 'admin.edit');
            if (true !== $result) {
                $this->error($result);
            }

            if ($data['password'] == '' || is_null($data['password'])) {
                unset($data['password']);
            }
            if (!empty($data['group_id'])) {
                $data['group_id'] = implode(',', $data['group_id']);
            }
            if ($this->model->where(['id'=>$data['id']])->update($data) !== false) {
                $this->success('保存成功', Url('index'));
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $this->error('请求错误');
        }
    }

    // 修改密码
    public function editpwd()
    {
        $id  = get_admin_id();
        $row = $this->model->where('id',$id)->find();
        if (empty($row)) {
            $this->error('数据不存在');
        }
        $row = $row->toArray();

        $this->assign($row);
        return $this->fetch();
    }

    public function pwd_post()
    {
        if (request()->isPost()) {
            $data = input('post.');

            if (empty($data['password'])) {
                $this->error('密码不能为空');
            }
            $data['password'] =  Hash::make((string)$data['password']);
            $id = $data['id'];
            unset($data['id']);

            if ($this->model->where(['id'=>$id])->update($data) !== false) {
                $this->success('保存成功', Url('editpwd'));
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $this->error('请求错误');
        }
    }
}
